package com.xxl.vueadmin.config.web;

import com.xxl.vueadmin.security.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import javax.annotation.Resource;

/**
 * @version 1.0
 * @Author: 许啸林 spring-security 配置类
 * @Date: 2021/7/29 15:15
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ScurityConfig extends WebSecurityConfigurerAdapter {
    public static String[] URL_WRITELIST = {
            "/login",
            "/loginout",
            "/getCaptcha",
            "/favicon.ico",
            "/pass"
    };

    @Autowired
    LoginSuccessHandler loginSuccessHandel;
    @Autowired
    LoginFailureHandler loginFailureHandel;
    @Autowired
    CaptchaFilter captchaFilter;
    @Autowired
    JwtAuthorticationException jwtAuthorticationException;
    @Autowired
    JwtAccessDeineException jwtAccessDeineException;
    @Autowired
    JwtLogoutHandler jwtLogoutHandler;

    @Resource
    UserDetailServiceImpl userDetailService;
    @Bean
    JwtAuthenticationFilter jwtAuthenticationFilter() throws Exception {
        JwtAuthenticationFilter jwtAuthenticationFilter = new JwtAuthenticationFilter(authenticationManager());
        return jwtAuthenticationFilter;
    }

    @Bean
    BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors()
             .and().csrf().disable()
             .formLogin()
               .successHandler(loginSuccessHandel)
               .failureHandler(loginFailureHandel)
             .and().logout()
             .logoutSuccessHandler(jwtLogoutHandler)
             .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
             .and().authorizeRequests()
               .antMatchers(URL_WRITELIST).permitAll()
               .anyRequest().authenticated()
             // 自定义异常处理
             .and().exceptionHandling()
                .authenticationEntryPoint(jwtAuthorticationException)
                .accessDeniedHandler(jwtAccessDeineException)
              // 自定义过滤器
             .and()
               // jwt鉴权过滤器
               .addFilter(jwtAuthenticationFilter())
               // 验证码过滤器 在用户名密码过滤器之前
               .addFilterBefore(captchaFilter, UsernamePasswordAuthenticationFilter.class);



    }

    /**
     * 自定义用户获取实现类
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailService);
    }
}
